package engine.input;

public class KeyUnit {
    boolean pressed;//按下
    boolean released;//松开 这两个在帧更新完成后会复位为false
    boolean held;//按住不松
    int typeCount;//敲击
    int typeStore;//在一定时间内的敲击次数
    long typeStamp;//时间
    final int TYPE_DURATION = 200;

    //按下一个按键
    public void press() {
        if (!held) {
            pressed = true;
            held = true;
        }
    }

    //松开一个按键
    public void release() {
        if (held) {
            released = true;
            held = false;
            typeCount = ++typeStore;
            typeStamp = System.currentTimeMillis();
        }
    }

    //刷新方法，在帧更新完成后调用
    public void refresh() {
        pressed = false;
        released = false;
        typeCount = 0;
        if (typeStamp > 0) {
            long now = System.currentTimeMillis();
            long stamp = typeStamp;
            if (now - stamp > TYPE_DURATION) {
                typeStamp = 0;
                typeStore = 0;
            }
        }
    }

    //重置方法，在游戏开始时调用
    public void reset() {
        pressed = false;
        released = false;
        held = false;
        typeCount = 0;
        typeStore = 0;
        typeStamp = 0;
    }
}
